package com.chen.servicepassenger.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chen.internalcommon.constant.CommonStatusEnum;
import com.chen.internalcommon.dto.ResponseResult;
import com.chen.internalcommon.dto.UserInfo;
import com.chen.servicepassenger.entity.PassengerUser;
import com.chen.servicepassenger.mapper.PassengerUserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author chenLiuPing
 **/
@Service
@Slf4j
public class UserService {
    @Autowired
    private PassengerUserMapper passengerUserMapper;


    public ResponseResult loginOrRegister(String passengerPhone) {
        System.out.println("乘客手机号：" + passengerPhone);

        // 根据手机号查询用户是否存在
        QueryWrapper<PassengerUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("passenger_phone", passengerPhone);
        PassengerUser selectPassengerUser = passengerUserMapper.selectOne(queryWrapper);
        // 如果不存在则插入
        if (selectPassengerUser == null) {
            PassengerUser passengerUser = PassengerUser.generate(passengerPhone);
            passengerUserMapper.insert(passengerUser);
        }
        return ResponseResult.success();
    }

    public ResponseResult getUser(String passengerPhone) {
        log.info("passengerPhone:{}", passengerPhone);
        // 根据手机号查询用户是否存在
        QueryWrapper<PassengerUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("passenger_phone", passengerPhone);
        PassengerUser selectPassengerUser = passengerUserMapper.selectOne(queryWrapper);
        if (selectPassengerUser == null) {
            return ResponseResult.fail(CommonStatusEnum.USER_NOT_EXISTS);
        }

        UserInfo userInfo = new UserInfo(selectPassengerUser.getPassengerName(),
                selectPassengerUser.getProfilePhoto(),
                selectPassengerUser.getId(),
                selectPassengerUser.getPassengerPhone());
        return ResponseResult.success(userInfo);
    }
}
